package com.lzx.iteam;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.lzx.iteam.net.AsynHttpClient;
import com.lzx.iteam.net.LocalLogin;
import com.lzx.iteam.net.MyMessage;
import com.lzx.iteam.provider.CloudDBOperation;
import com.lzx.iteam.util.AllDialogUtil;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangshuai on 15/11/5.
 */
public class CodeResultActivity extends Activity implements OnClickListener{

    private LinearLayout mLlBack;
    private TextView mTvReg;
    private WebView mRegWeb;
    private Dialog mDialog;
    private String url;
    private String eventId;

    public static final int REG_EVENT = 1001;

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case REG_EVENT:
                    if (msg.arg1 == 0){
                        JSONObject result = (JSONObject) msg.obj;
                        try {
                            String code = result.getString("late");
                            if ("0".equals(code)){ // 无权限(不是里面的成员)
                                Toast.makeText(CodeResultActivity.this, "您不是参会人员,不能签到",
                                        Toast.LENGTH_LONG).show();
                            }else if ("1".equals(code)){ // 已经签过到(不要重复签到)
                                Toast.makeText(CodeResultActivity.this, "您已经签过到了",
                                        Toast.LENGTH_LONG).show();
                            }else if ("2".equals(code)){ // 正常签到
                                Toast.makeText(CodeResultActivity.this, "签到成功",
                                        Toast.LENGTH_LONG).show();
                            }else if ("3".equals(code)){ // 晚签到
                                Toast.makeText(CodeResultActivity.this, "签到成功",
                                        Toast.LENGTH_LONG).show();
                            }else if ("4".equals(code)){ // 还没到签到时间
                                Toast.makeText(CodeResultActivity.this, "会议签到尚未开始,请稍后签到",
                                        Toast.LENGTH_LONG).show();
                            }else if ("5".equals(code)){ // 签到通道关闭
                                Toast.makeText(CodeResultActivity.this, "抱歉，该会议签到已经结束",
                                        Toast.LENGTH_LONG).show();
                            }
                            mTvReg.setEnabled(false);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }else {

                        if (msg.arg1 == AsynHttpClient.ERROR_LOCAL) {
                            Toast.makeText(CodeResultActivity.this, R.string.active_failure_no_network,
                                    Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(CodeResultActivity.this,(String)msg.obj,
                                    Toast.LENGTH_LONG).show();
                        }
                    }
                    break;
            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.code_resuit_layout);
        initView();
        Intent intent = getIntent();
        url = intent.getStringExtra("reg_url");
        eventId = url.split("event_id=")[1];
        WebSettings setting = mRegWeb.getSettings();
        setting.setJavaScriptEnabled(true);//支持js
        mDialog = AllDialogUtil.getInstance(this).waitDialog();
        mDialog.show();
        mRegWeb.setWebChromeClient(new WebChromeClient(){
            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                return super.onJsAlert(view, url, message, result);
            }
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                //setProgress(newProgress*100);
                if (newProgress == 100){
                    mDialog.dismiss();
                }
            }
        });
        mRegWeb.loadUrl(url);

    }
    public void initView(){
        mLlBack = (LinearLayout) findViewById(R.id.code_ll_back);
        mTvReg = (TextView) findViewById(R.id.code_tv_reg);
        mRegWeb = (WebView) findViewById(R.id.code_wv_result);
        mLlBack.setOnClickListener(this);
        mTvReg.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.code_ll_back:
                finish();
                break;
            case R.id.code_tv_reg:
                eventReg();
                break;
        }
    }
    public void eventReg(){
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_SID, LocalLogin
                .getInstance().mSid));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_EVENT_ID, eventId));
        MyMessage.EventRegMessage message = new MyMessage.EventRegMessage(mHandler.obtainMessage(REG_EVENT));
        message.mApi = AsynHttpClient.API_EVENT_REG;
        message.mParams = nvps;
        AsynHttpClient.getInstance(this).execEventHttp(message);
    }

}
